package com.glwan.fixedassetapp.utils;

import static com.glwan.fixedassetapp.common.ExcelTemplateConstant.ASSET_IMPORT_TEMPLATE_COLUMN_ARRAY;
import static com.glwan.fixedassetapp.common.ExcelTemplateConstant.ASSET_IMPORT_TEMPLATE_COLUMN_ARRAY_NEW;

import android.database.Cursor;

import androidx.annotation.NonNull;

import com.glwan.fixedassetapp.common.CommonConstant;
import com.glwan.fixedassetapp.common.DatabaseConstant;
import com.glwan.fixedassetapp.model.AssetOld;
import com.glwan.fixedassetapp.model.Asset;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class OffsetAssetUtils {

    public static Integer getTotalCount(String selection, String[] selectionArgArray) {
        Cursor totalCursor = DatabaseUtil.rawQuery("SELECT COUNT(1) FROM " + DatabaseConstant.OFFLINE_INVENTORY_TASK_ITEM + (StringUtils.isNotEmpty(selection) ? " WHERE  " + selection : selection), selectionArgArray);
        totalCursor.moveToFirst();
        return totalCursor.getInt(0);
    }

    public static List<Asset> getAssetListDataNew(int currentCount, String selection, String[] selectionArgArray) {
        List<Asset> list = new ArrayList<>();
        Cursor cursor = DatabaseUtil.querySQL(
                DatabaseConstant.OFFLINE_INVENTORY_TASK_ITEM,
                ASSET_IMPORT_TEMPLATE_COLUMN_ARRAY_NEW.toArray(new String[0]),
                selection,
                selectionArgArray,
                null,
                currentCount + "," + CommonConstant.INIT_LIST_VIEW_SIZE);
        while (cursor.moveToNext()) {
            Asset info = fromCursorNew(cursor);
            list.add(info);
        }
        return list;
    }



    public static AssetOld fromCursor(@NonNull Cursor cursor) {
        AssetOld asset = new AssetOld();
        int indexID = cursor.getColumnIndex("ID");
        if (indexID != -1) {
            asset.setId(cursor.getString(indexID));
        }
        int indexCompanyCode = cursor.getColumnIndex("CompanyCode");
        if (indexCompanyCode != -1) {
            asset.setCompanyCode(cursor.getString(indexCompanyCode));
        }
        int indexFiscalYear = cursor.getColumnIndex("FiscalYear");
        if (indexFiscalYear != -1) {
            asset.setFiscalYear(cursor.getString(indexFiscalYear));
        }
        int indexSerialNumber = cursor.getColumnIndex("SerialNumber");
        if (indexSerialNumber != -1) {
            asset.setSerialNumber(cursor.getString(indexSerialNumber));
        }
        int indexAssetName = cursor.getColumnIndex("AssetName");
        if (indexAssetName != -1) {
            asset.setAssetName(cursor.getString(indexAssetName));
        }
        int indexCostCenter = cursor.getColumnIndex("CostCenter");
        if (indexCostCenter != -1) {
            asset.setCostCenter(cursor.getString(indexCostCenter));
        }
        int indexCostCenterName = cursor.getColumnIndex("CostCenterName");
        if (indexCostCenterName != -1) {
            asset.setCostCenterName(cursor.getString(indexCostCenterName));
        }
        int indexAssetCatalogCode = cursor.getColumnIndex("AssetCatalogCode");
        if (indexAssetCatalogCode != -1) {
            asset.setAssetCatalogCode(cursor.getString(indexAssetCatalogCode));
        }
        int indexAssetCatalogName = cursor.getColumnIndex("AssetCatalogName");
        if (indexAssetCatalogName != -1) {
            asset.setAssetCatalogName(cursor.getString(indexAssetCatalogName));
        }
        int indexAssetSerialNumber = cursor.getColumnIndex("AssetSerialNumber");
        if (indexAssetSerialNumber != -1) {
            asset.setAssetSerialNumber(cursor.getString(indexAssetSerialNumber));
        }
        int indexSpecificationModel = cursor.getColumnIndex("SpecificationModel");
        if (indexSpecificationModel != -1) {
            asset.setSpecificationModel(cursor.getString(indexSpecificationModel));
        }
        int indexQuantity = cursor.getColumnIndex("Quantity");
        if (indexQuantity != -1) {
            asset.setQuantity(cursor.getString(indexQuantity));
        }
        int indexMeasurementUnit = cursor.getColumnIndex("MeasurementUnit");
        if (indexMeasurementUnit != -1) {
            asset.setMeasurementUnit(cursor.getString(indexMeasurementUnit));
        }
        int indexPlaceOfPreservation = cursor.getColumnIndex("PlaceOfPreservation");
        if (indexPlaceOfPreservation != -1) {
            asset.setPlaceOfPreservation(cursor.getString(indexPlaceOfPreservation));
        }
        int indexAssetUsageStatus = cursor.getColumnIndex("AssetUsageStatus");
        if (indexAssetUsageStatus != -1) {
            asset.setAssetUsageStatus(cursor.getString(indexAssetUsageStatus));
        }
        int indexUsingDepartment = cursor.getColumnIndex("UsingDepartment");
        if (indexUsingDepartment != -1) {
            asset.setUsingDepartment(cursor.getString(indexUsingDepartment));
        }
        int indexUser = cursor.getColumnIndex("User");
        if (indexUser != -1) {
            asset.setUser(cursor.getString(indexUser));
        }
        int indexResponsiblePerson = cursor.getColumnIndex("ResponsiblePerson");
        if (indexResponsiblePerson != -1) {
            asset.setResponsiblePerson(cursor.getString(indexResponsiblePerson));
        }
        int indexSupplier = cursor.getColumnIndex("Supplier");
        if (indexSupplier != -1) {
            asset.setSupplier(cursor.getString(indexSupplier));
        }
        int indexSupplierName = cursor.getColumnIndex("SupplierName");
        if (indexSupplierName != -1) {
            asset.setSupplierName(cursor.getString(indexSupplierName));
        }
        int indexEnteredBy = cursor.getColumnIndex("EnteredBy");
        if (indexEnteredBy != -1) {
            asset.setEnteredBy(cursor.getString(indexEnteredBy));
        }
        int indexEntryDate = cursor.getColumnIndex("EntryDate");
        if (indexEntryDate != -1) {
            asset.setEntryDate(cursor.getString(indexEntryDate));
        }
        int indexInventoryStatus = cursor.getColumnIndex("InventoryStatus");
        if (indexInventoryStatus != -1) {
            asset.setInventoryStatus(cursor.getString(indexInventoryStatus));
        }
        return asset;
    }

    public static Asset fromCursorNew(@NonNull Cursor cursor) {
        Asset asset = new Asset();
        int indexNo = cursor.getColumnIndex("no");
        if (indexNo != -1) {
            asset.setNo(cursor.getString(indexNo));
        }
        int indexAssetNumber = cursor.getColumnIndex("assetNumber");
        if (indexAssetNumber != -1) {
            asset.setAssetNumber(cursor.getString(indexAssetNumber));
        }
        int indexAssetName = cursor.getColumnIndex("assetName");
        if (indexAssetName != -1) {
            asset.setAssetName(cursor.getString(indexAssetName));
        }
        int indexSpecs = cursor.getColumnIndex("specs");
        if (indexSpecs != -1) {
            asset.setSpecs(cursor.getString(indexSpecs));
        }
        int indexModel = cursor.getColumnIndex("model");
        if (indexModel != -1) {
            asset.setModel(cursor.getString(indexModel));
        }
        int indexBrand = cursor.getColumnIndex("brand");
        if (indexBrand != -1) {
            asset.setBrand(cursor.getString(indexBrand));
        }
        int indexSerialNumber = cursor.getColumnIndex("serialNumber");
        if (indexSerialNumber != -1) {
            asset.setSerialNumber(cursor.getString(indexSerialNumber));
        }
        int indexAssetCategory = cursor.getColumnIndex("assetCategory");
        if (indexAssetCategory != -1) {
            asset.setAssetCategory(cursor.getString(indexAssetCategory));
        }
        int indexUnit = cursor.getColumnIndex("unit");
        if (indexUnit != -1) {
            asset.setUnit(cursor.getString(indexUnit));
        }
        int indexQuantity = cursor.getColumnIndex("quantity");
        if (indexQuantity != -1) {
            asset.setQuantity(cursor.getString(indexQuantity));
        }
        int indexPriceExcludingTax = cursor.getColumnIndex("priceExcludingTax");
        int indexTaxRate = cursor.getColumnIndex("taxRate");
        if (indexTaxRate != -1) {
            asset.setTaxRate(cursor.getString(indexTaxRate));
        }
        int indexPurchaseDate = cursor.getColumnIndex("purchaseDate");
        if (indexPurchaseDate != -1) {
            asset.setPurchaseDate(cursor.getString(indexPurchaseDate));
        }
        int indexStoreLocation = cursor.getColumnIndex("storeLocation");
        if (indexStoreLocation != -1) {
            asset.setStoreLocation(cursor.getString(indexStoreLocation));
        }
        int indexUsingDepartment = cursor.getColumnIndex("usingDepartment");
        if (indexUsingDepartment != -1) {
            asset.setUsingDepartment(cursor.getString(indexUsingDepartment));
        }
        int indexUser = cursor.getColumnIndex("user");
        if (indexUser != -1) {
            asset.setUser(cursor.getString(indexUser));
        }
        int indexUsageStatus = cursor.getColumnIndex("usageStatus");
        if (indexUsageStatus != -1) {
            asset.setUsageStatus(cursor.getString(indexUsageStatus));
        }
        int indexMark = cursor.getColumnIndex("mark");
        if (indexMark != -1) {
            asset.setMark(cursor.getString(indexMark));
        }
        int indexInventoryStatus = cursor.getColumnIndex("inventoryStatus");
        if (indexInventoryStatus != -1) {
            asset.setInventoryStatus(cursor.getString(indexInventoryStatus));
        }
        return asset;
    }

}
